﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using YJKArchBaseCode.OutWallSearch;
using YJKArchUtils.Utils;
using YJKRevitKernel.RevitDimensionUtility;
using YJKGeometryFuncs;
using YJKRevitDimensionText.DimCreator;
using YJKRevitDimensionText.OneKeyPlanDim.DimesionDataEx;
using YJKRevitDimensionText.OneKeyPlanDim.LevelDim;
using YJKRevitDimensionText.OneKeyPlanDim.OutWallThreeDim.Data;
using YJKRevitDimensionText.OneKeyPlanDim.Utility;
using YJKRevitDimensionText.OneKeyPlanDim.Utility.XDataUtils;
using YJKRevitDimensionText.VerticalAutoDim.OutWallSearch;

namespace YJKRevitDimensionText.OneKeyPlanDim.OutWallThreeDim
{
	public class OutWallThreeDimManager
	{
		public OutWallThreeDimManager(Autodesk.Revit.DB.Document doc, UIApplication uiApp, Autodesk.Revit.DB.View view, List<WallDataEx> lstWallEx = null)
		{
			this.m_doc = doc;
			this.m_uiApp = uiApp;
			this.m_view = view;
			this.m_lstWallDataEx = lstWallEx;
		}

		public List<ElementId> CreateOutWallThreeDimesion(ref Dictionary<ElementId, ElementId> dicWallDim)
		{
			List<ElementId> list = new List<ElementId>();
			if (!OneKeyPlanDimSelctionDim.m_bOutArchSize)
			{
				return list;
			}
			try
			{
				List<Wall> list2 = new List<Wall>();
				foreach (OutWallData outWallData in OutRoomOutWallCreator.GetOutermostWalls(new OutRoomOutWallCreator.DetermineAdjacentElementLengthsAndWallAreasDel(OutRoomOutWallCreator.DetermineAdjacentElementLengthsAndWallAreasEx), this.m_doc, this.m_view, true))
				{
					Wall wall = outWallData.Element as Wall;
					if (wall != null && !this.HasSplitWall(list2, wall))
					{
						list2.Add(wall);
					}
				}
				List<List<Wall>> lstDirWallArr = new List<List<Wall>>();
				this.GetDirWallArray(list2, lstDirWallArr);
				List<Dimension> list3 = new List<Dimension>();
				this.CreateOutWallThickSizeDim(list2, list3, ref dicWallDim);
				this.DimesionAvoid(list3);
				foreach (Dimension dimension in list3)
				{
					list.Add(dimension.Id);
				}
			}
			catch (Exception)
			{
				return list;
			}
			return list;
		}

		private void CreateProfileGirdSizeDim(List<Wall> lstAllOutWall, List<Dimension> lstAllSizeDim)
		{
			using (Transaction transaction = new Transaction(this.m_doc, "newProfileDim"))
			{
				transaction.Start();
				try
				{
					foreach (List<Grid> list in this.GetDirGridArr(this.m_doc, this.m_view.Id))
					{
						if (list.Count >= 2)
						{
							ReferenceArray referenceArray = new ReferenceArray();
							foreach (Grid grid in list)
							{
								Reference reference = null;
								GridOperationUtil.GetGridDimRef(this.m_uiApp, grid, out reference);
								if (reference != null)
								{
									referenceArray.Append(reference);
								}
							}
							ReferenceArray referenceArray2 = new ReferenceArray();
							List<Wall> list2 = new List<Wall>();
							Grid grid2 = list.First<Grid>();
							Grid grid3 = list.Last<Grid>();
							Wall wallByGrid = GridOperationUtil.GetWallByGrid(grid2, lstAllOutWall);
							if (wallByGrid != null)
							{
								list2.Add(wallByGrid);
							}
							else
							{
								Reference reference2 = null;
								GridOperationUtil.GetGridDimRef(this.m_uiApp, grid2, out reference2);
								if (reference2 != null)
								{
									referenceArray2.Append(reference2);
								}
							}
							Wall wallByGrid2 = GridOperationUtil.GetWallByGrid(grid3, lstAllOutWall);
							if (wallByGrid2 != null)
							{
								list2.Add(wallByGrid2);
							}
							else
							{
								Reference reference3 = null;
								GridOperationUtil.GetGridDimRef(this.m_uiApp, grid3, out reference3);
								if (reference3 != null)
								{
									referenceArray2.Append(reference3);
								}
							}
							if (list2.Count > 0)
							{
								foreach (Wall wall in list2)
								{
									Reference wallProfileRef = WallOperattionUtil.GetWallProfileRef(this.m_uiApp, wall);
									if (wallProfileRef != null)
									{
										referenceArray2.Append(wallProfileRef);
									}
								}
							}
							XYZ gridDirection = GridOperationUtil.GetGridDirection(grid2);
							XYZ xyz = Transform.CreateRotation(XYZ.BasisZ, Math.PI*.5).OfVector(gridDirection);
							XYZ xyz2 = this.GetGirdDimPoint(list, gridDirection, xyz) + gridDirection.Multiply(2.6246719160104992);
							if (xyz2 != null)
							{
								Dimension dimension = this.CreateSizeDim(referenceArray2, xyz, xyz2);
								if (dimension != null)
								{
									lstAllSizeDim.Add(dimension);
								}
								dimension = this.CreateSizeDim(referenceArray, xyz, xyz2 - gridDirection.Multiply(2.6246719160104992));
								if (dimension != null)
								{
									lstAllSizeDim.Add(dimension);
								}
							}
							xyz2 = this.GetGirdDimPoint(list, -gridDirection, xyz) - gridDirection.Multiply(2.6246719160104992);
							if (xyz2 != null)
							{
								Dimension dimension2 = this.CreateSizeDim(referenceArray2, xyz, xyz2);
								if (dimension2 != null)
								{
									lstAllSizeDim.Add(dimension2);
								}
								dimension2 = this.CreateSizeDim(referenceArray, xyz, xyz2 + gridDirection.Multiply(2.6246719160104992));
								if (dimension2 != null)
								{
									lstAllSizeDim.Add(dimension2);
								}
							}
						}
					}
					transaction.Commit();
				}
				catch (Exception)
				{
					transaction.RollBack();
				}
			}
		}

		private XYZ GetGirdDimPoint(List<Grid> lstGird, XYZ vecGrid, XYZ vecLine)
		{
			XYZ result = null;
			Line line = Line.CreateBound(XYZ.Zero, vecLine);
			line.MakeUnbound();
			double num = double.MaxValue;
			foreach (Grid grid in lstGird)
			{
				XYZ lineGridSameDirPoint = GridOperationUtil.GetLineGridSameDirPoint(grid, vecGrid);
				if (lineGridSameDirPoint != null)
				{
					XYZ xyzpoint = line.Project(lineGridSameDirPoint).XYZPoint;
					double num2 = lineGridSameDirPoint.DistanceTo(xyzpoint);
					if (num2 < num)
					{
						result = lineGridSameDirPoint;
						num = num2;
					}
				}
			}
			return result;
		}

		private void CreateOutWallThickSizeDim(List<Wall> lstAllOutWall, List<Dimension> lstAllSizeDim, ref Dictionary<ElementId, ElementId> dicWallDim)
		{
			Transaction transaction = new Transaction(this.m_doc, "thickSizeDim");
			transaction.Start();
			try
			{
				List<Grid> allLineGrid = GridOperationUtil.GetAllLineGrid(this.m_doc, this.m_view.Id);
				List<List<Wall>> list = new List<List<Wall>>();
				this.GetLocWallArray(lstAllOutWall, list);
				foreach (List<Wall> lstWall in list)
				{
					OutWallThickDimManager outWallThickDimManager = new OutWallThickDimManager(this.m_uiApp, this.m_doc, this.m_view, lstWall, allLineGrid, this.m_lstWallDataEx);
					List<Dimension> list2 = outWallThickDimManager.CreateOutWallThickDim();
					if (list2.Count > 0)
					{
						lstAllSizeDim.AddRange(list2);
						foreach (KeyValuePair<ElementId, ElementId> keyValuePair in outWallThickDimManager.DicWallDim)
						{
							dicWallDim.Add(keyValuePair.Key, keyValuePair.Value);
						}
					}
				}
			}
			catch (Exception)
			{
				transaction.RollBack();
			}
			transaction.Commit();
		}

		private List<Dimension> CreateProfileDim()
		{
			List<Dimension> list = new List<Dimension>();
			Transaction transaction = new Transaction(this.m_doc, "createDim");
			transaction.Start();
			try
			{
				List<Wall> lstWall = new List<Wall>();
				OutWallFilter.Search(this.m_doc, this.m_view.Id, ref lstWall, true, false);
				Wall xMinWall = null;
				Wall wall = null;
				Wall xMinWall2 = null;
				Wall wall2 = null;
				OutWallThreeDimManager.GetMaxDisWall(lstWall, out xMinWall, out wall, true);
				OutWallThreeDimManager.GetMaxDisWall(lstWall, out xMinWall2, out wall2, false);
				XYZ wallLocationCenter = WallOperattionUtil.GetWallLocationCenter(wall2);
				Dimension dimension = this.CreateWallDirDim(xMinWall, wall, wallLocationCenter.Y, true);
				if (dimension != null)
				{
					list.Add(dimension);
				}
				wallLocationCenter = WallOperattionUtil.GetWallLocationCenter(wall);
				dimension = this.CreateWallDirDim(xMinWall2, wall2, wallLocationCenter.X, false);
				if (dimension != null)
				{
					list.Add(dimension);
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
			}
			return list;
		}

		private Dimension CreateWallDirDim(Wall xMinWall, Wall xMaxWall, double dMaxValue, bool bIsWidth = true)
		{
			if (xMinWall == null || xMaxWall == null)
			{
				return null;
			}
			ReferenceArray referenceArray = new ReferenceArray();
			Reference wallProfileRef = WallOperattionUtil.GetWallProfileRef(this.m_uiApp, xMinWall);
			if (wallProfileRef != null)
			{
				referenceArray.Append(wallProfileRef);
			}
			wallProfileRef = WallOperattionUtil.GetWallProfileRef(this.m_uiApp, xMaxWall);
			if (wallProfileRef != null)
			{
				referenceArray.Append(wallProfileRef);
			}
			XYZ vtDim = xMinWall.Orientation.Normalize();
			XYZ wallLocVector = WallOperattionUtil.GetWallLocVector(xMinWall);
			double dTrans = 2.6246719160104992;
			XYZ xyz;
			if (bIsWidth)
			{
				xyz = GridOperationUtil.GetYDirMinGridUpEndPoint(this.m_doc, this.m_view.Id, wallLocVector, dMaxValue, dTrans);
			}
			else
			{
				xyz = GridOperationUtil.GetXDirMinGridRightEndPoint(this.m_doc, this.m_view.Id, wallLocVector, dMaxValue, dTrans);
			}
			if (xyz == null)
			{
				return null;
			}
			return this.CreateSizeDim(referenceArray, vtDim, xyz);
		}

		private static void GetMaxDisWall(List<Wall> lstWall, out Wall minWall, out Wall maxWall, bool bIsX = true)
		{
			minWall = null;
			maxWall = null;
			double num = double.MaxValue;
			foreach (Wall wall in lstWall)
			{
				XYZ wallLocationCenter = WallOperattionUtil.GetWallLocationCenter(wall);
				double num2 = wallLocationCenter.X;
				if (!bIsX)
				{
					num2 = wallLocationCenter.Y;
				}
				if (num2 < num)
				{
					minWall = wall;
					num = num2;
				}
			}
			if (minWall == null)
			{
				return;
			}
			double num3 = double.MinValue;
			XYZ wallLocVector = WallOperattionUtil.GetWallLocVector(minWall);
			foreach (Wall wall2 in lstWall)
			{
				if (wall2.Id.IntegerValue != minWall.Id.IntegerValue)
				{
					XYZ wallLocVector2 = WallOperattionUtil.GetWallLocVector(wall2);
                    if (YJKArch.Utils.Geometry.IsParallel(wallLocVector, wallLocVector2))
					{
						double twoWallDistance = WallOperattionUtil.GetTwoWallDistance(minWall, wall2);
						if (twoWallDistance > num3)
						{
							maxWall = wall2;
							num3 = twoWallDistance;
						}
					}
				}
			}
		}

		private List<Dimension> CreateInterGridDim()
		{
			return new List<Dimension>();
		}

		private List<Dimension> CreateOutWallWinDoorDim()
		{
			return new List<Dimension>();
		}

		private List<Dimension> CreateWallThickDim()
		{
			return new List<Dimension>();
		}

		private void DimesionAvoid(List<Dimension> lstDim)
		{
			try
			{
				foreach (Dimension inputDim in lstDim)
				{
					DimAvoidManager.ClearCache();
					DimAvoidManager.TryAutoAvoidForHorizontal(this.m_doc, inputDim);
				}
				DimAvoidManager.TryAutoAvoidElementForHorizontal(this.m_doc, lstDim);
			}
			catch (Exception)
			{
			}
		}

		private List<OutWallThreeDimData> GetAllThreeOutWallDimData(List<List<Wall>> lstDirWallArr)
		{
			List<OutWallThreeDimData> list = new List<OutWallThreeDimData>();
			try
			{
				List<Wall> list2 = new List<Wall>();
				foreach (List<Wall> source in lstDirWallArr)
				{
					list2.Add(source.Last<Wall>());
				}
				foreach (Wall wall in list2)
				{
					DirectionOutWallData dirOutWallData = this.GetDirOutWallData(lstDirWallArr, list2, wall);
					List<Grid> vectorParallelGrid = GridOperationUtil.GetVectorParallelGrid(this.m_doc, this.m_view.Id, wall.Orientation.Normalize());
					List<Grid> list3 = new List<Grid>();
					Line line = OutWallThreeDimManager.GetZeroHeightCurve((wall.Location as LocationCurve).Curve) as Line;
					line.MakeUnbound();
					foreach (Grid grid in vectorParallelGrid)
					{
						IntersectionResultArray intersectionResultArray = null;
						OutWallThreeDimManager.GetZeroHeightCurve(grid.Curve).Intersect(line, out intersectionResultArray);
						if (intersectionResultArray != null && 1 == intersectionResultArray.Size)
						{
							list3.Add(grid);
						}
					}
					Dictionary<XYZ, Grid> dictionary = new Dictionary<XYZ, Grid>();
					foreach (Grid grid2 in list3)
					{
						XYZ lineGridCenter = GridOperationUtil.GetLineGridCenter(grid2);
						dictionary.Add(lineGridCenter, grid2);
					}
					List<XYZ> list4 = this.SortPointsWithVector(dictionary.Keys.ToList<XYZ>(), line.Direction.Normalize());
					List<Grid> list5 = new List<Grid>();
					foreach (XYZ key in list4)
					{
						list5.Add(dictionary[key]);
					}
					if (list5.Count == list3.Count)
					{
						list3 = list5;
					}
					XYZ xyz = null;
					double num = double.MaxValue;
					foreach (Grid grid3 in list3)
					{
						XYZ lineGridSameDirPoint = GridOperationUtil.GetLineGridSameDirPoint(grid3, wall.Orientation.Normalize());
						if (lineGridSameDirPoint != null)
						{
							XYZ xyzpoint = line.Project(lineGridSameDirPoint).XYZPoint;
							double num2 = lineGridSameDirPoint.DistanceTo(xyzpoint);
							if (num2 < num)
							{
								xyz = lineGridSameDirPoint;
								num = num2;
							}
						}
					}
					if (xyz != null)
					{
						XYZ xyz2 = -(WallOperattionUtil.GetPointToWallCurve(xyz, wall) as Line).Direction.Normalize().Multiply(15.748031496062991 + wall.Width);
						xyz = WallOperattionUtil.GetWallLocationCenter(wall) + xyz2;
					}
					List<Grid> vectorParallelGrid2 = GridOperationUtil.GetVectorParallelGrid(this.m_doc, this.m_view.Id, line.Direction.Normalize());
					OutWallGridData gridData = new OutWallGridData(xyz, list3, vectorParallelGrid2);
					OutWallThreeDimData item = new OutWallThreeDimData(dirOutWallData, gridData);
					list.Add(item);
				}
			}
			catch (Exception)
			{
			}
			return list;
		}

		private DirectionOutWallData GetDirOutWallData(List<List<Wall>> lstDirWallArr, List<Wall> lstFurthestWall, Wall wall)
		{
			List<Wall> list = new List<Wall>();
			foreach (Wall wall2 in lstFurthestWall)
			{
				if (YJKArch.Utils.Geometry.IsVertical(wall2.Orientation.Normalize(), wall.Orientation.Normalize()))
				{
					list.Add(wall2);
				}
			}
			List<Wall> lstParallelWall = new List<Wall>();
			foreach (List<Wall> list2 in lstDirWallArr)
			{
				XYZ xyz = list2.First<Wall>().Orientation.Normalize();
				if (wall.Orientation.Normalize().IsAlmostEqualTo(xyz))
				{
					lstParallelWall = list2;
					break;
				}
			}
			return new DirectionOutWallData(wall, list, lstParallelWall);
		}

		private List<List<Grid>> GetDirGridArr(Autodesk.Revit.DB.Document doc, ElementId viewId)
		{
			List<List<Grid>> list = new List<List<Grid>>();
			List<Grid> allLineGrid = GridOperationUtil.GetAllLineGrid(doc, viewId);
			foreach (Grid grid in allLineGrid)
			{
				if (!this.HasGridArr(grid, list))
				{
					XYZ gridDirection = GridOperationUtil.GetGridDirection(grid);
					List<Grid> list2 = new List<Grid>();
					foreach (Grid grid2 in allLineGrid)
					{
						if (!this.HasGridArr(grid2, list))
						{
							XYZ gridDirection2 = GridOperationUtil.GetGridDirection(grid2);
							if (YJKArch.Utils.Geometry.IsParallel(gridDirection, gridDirection2))
							{
								list2.Add(grid2);
							}
						}
					}
					if (list2.Count > 1)
					{
						Dictionary<XYZ, Grid> dictionary = new Dictionary<XYZ, Grid>();
						foreach (Grid grid3 in list2)
						{
							XYZ lineGridCenter = GridOperationUtil.GetLineGridCenter(grid3);
							dictionary.Add(lineGridCenter, grid3);
						}
						XYZ vecDirection = Transform.CreateRotation(XYZ.BasisZ, Math.PI*.5).OfVector(gridDirection);
						List<XYZ> srcPoints = dictionary.Keys.ToList<XYZ>();
						List<XYZ> list3 = this.SortPointsWithVector(srcPoints, vecDirection);
						List<Grid> list4 = new List<Grid>();
						foreach (XYZ key in list3)
						{
							Grid item = dictionary[key];
							list4.Add(item);
						}
						if (list4.Count > 1)
						{
							list.Add(list4);
						}
					}
				}
			}
			return list;
		}

		private void GetDirWallArray(List<Wall> lstAllOutWall, List<List<Wall>> lstDirWallArr)
		{
			List<Wall> list = new List<Wall>();
			foreach (Wall wall in lstAllOutWall)
			{
				if (!this.HasSplitWall(list, wall))
				{
					List<Wall> list2 = new List<Wall>();
					foreach (Wall wall2 in lstAllOutWall)
					{
						if (wall2.Orientation.Normalize().IsAlmostEqualTo(wall.Orientation.Normalize()))
						{
							list2.Add(wall2);
							list.Add(wall2);
						}
					}
					if (list2.Count > 0)
					{
						Dictionary<XYZ, Wall> dictionary = new Dictionary<XYZ, Wall>();
						foreach (Wall wall3 in list2)
						{
							XYZ wallLocationCenter = WallOperattionUtil.GetWallLocationCenter(wall3);
							dictionary.Add(wallLocationCenter, wall3);
						}
						XYZ vecDirection = list2.First<Wall>().Orientation.Normalize();
						List<XYZ> list3 = this.SortPointsWithVector(dictionary.Keys.ToList<XYZ>(), vecDirection);
						List<Wall> list4 = new List<Wall>();
						foreach (XYZ key in list3)
						{
							list4.Add(dictionary[key]);
						}
						if (list4.Count == list2.Count)
						{
							lstDirWallArr.Add(list4);
						}
					}
				}
			}
		}

		private void GetLocWallArray(List<Wall> lstValidOutWall, List<List<Wall>> lstDirWallArr)
		{
			foreach (Wall wall in lstValidOutWall)
			{
				if (!this.HasWallArr(wall, lstDirWallArr))
				{
					XYZ wallLocVector = WallOperattionUtil.GetWallLocVector(wall);
					List<Wall> list = new List<Wall>();
					foreach (Wall wall2 in lstValidOutWall)
					{
						XYZ wallLocVector2 = WallOperattionUtil.GetWallLocVector(wall2);
                        if (YJKArch.Utils.Geometry.IsParallel(wallLocVector, wallLocVector2))
						{
							list.Add(wall2);
						}
					}
					if (list.Count > 0)
					{
						lstDirWallArr.Add(list);
					}
				}
			}
		}

		private List<XYZ> SortPointsWithVector(List<XYZ> srcPoints, XYZ vecDirection)
		{
			List<XYZ> list = new List<XYZ>();
			List<XYZ> list2 = new List<XYZ>();
			list.AddRange(srcPoints);
			SortedList<double, List<XYZ>> sortedList = new SortedList<double, List<XYZ>>();
			XYZ xyz = vecDirection.Multiply(1.0);
			foreach (XYZ xyz2 in srcPoints)
			{
				double d = xyz.AngleTo(xyz2);
				double key = Math.Sqrt(xyz2.X * xyz2.X + xyz2.Y * xyz2.Y + xyz2.Z * xyz2.Z) * Math.Cos(d);
				if (!sortedList.ContainsKey(key))
				{
					sortedList.Add(key, new List<XYZ>
					{
						xyz2
					});
				}
				else
				{
					sortedList[key].Add(xyz2);
				}
			}
			if (sortedList.Count > 0)
			{
				IEnumerator<KeyValuePair<double, List<XYZ>>> enumerator2 = sortedList.GetEnumerator();
				while (enumerator2.MoveNext())
				{
					List<XYZ> list3 = list2;
					KeyValuePair<double, List<XYZ>> keyValuePair = enumerator2.Current;
					list3.AddRange(keyValuePair.Value);
				}
				if (list2.Count == srcPoints.Count)
				{
					list = list2;
				}
			}
			return list;
		}

		private bool HasSplitWall(List<Wall> lstSplitWall, Wall wall)
		{
			using (List<Wall>.Enumerator enumerator = lstSplitWall.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Id.IntegerValue == wall.Id.IntegerValue)
					{
						return true;
					}
				}
			}
			return false;
		}

		private List<Dimension> GetAllThreeOutWallDimesion(List<OutWallThreeDimData> lstDimData)
		{
			List<Dimension> list = new List<Dimension>();
			if (lstDimData.Count == 0)
			{
				return list;
			}
			foreach (OutWallThreeDimData outWallThreeDimData in lstDimData)
			{
				double num = Common.MMToFeet(800.0);
				XYZ xyz = outWallThreeDimData.DirOutWallData.FursthestWall.Orientation.Normalize();
				XYZ farthestDistanceInDir = this.GetFarthestDistanceInDir(KeyComponent.GetInstance().m_lstOutWallCurves, xyz);
				this.CreateOutWallProfileDim(list, outWallThreeDimData, farthestDistanceInDir + num * xyz * 3.0);
				this.CreateGridSizeDim(list, outWallThreeDimData, farthestDistanceInDir + num * xyz * 2.0);
				this.CreateWindowDoorSizeDim(list, outWallThreeDimData, farthestDistanceInDir + num * xyz);
			}
			return list;
		}

		private XYZ GetFarthestDistanceInDir(List<CurveLoop> lstCvLoop, XYZ vec)
		{
			XYZ result = XYZ.Zero;
			vec = vec.Normalize();
			Line line = Line.CreateUnbound(vec * 10000000.0, vec.CrossProduct(XYZ.BasisZ));
			double num = double.MaxValue;
			foreach (CurveLoop curveLoop in lstCvLoop)
			{
				foreach (Curve curve in curveLoop)
				{
					double num2 = line.Distance(RevitDocumentExtension.GetEndPoint(curve, 0));
                    if (YJKArch.Utils.Geometry.LessThan(num2, num))
					{
						num = num2;
						result = RevitDocumentExtension.GetEndPoint(curve, 0);
					}
				}
			}
			return result;
		}

		private XYZ GetFarthestDistanceInDir(List<Curve> lstCv, XYZ vec)
		{
			XYZ result = XYZ.Zero;
			vec = vec.Normalize();
			Line line = Line.CreateUnbound(vec * 10000000.0, vec.CrossProduct(XYZ.BasisZ));
			double num = double.MaxValue;
			foreach (Curve curve in lstCv)
			{
				double num2 = line.Distance(RevitDocumentExtension.GetEndPoint(curve, 0));
				if (YJKArch.Utils.Geometry.LessThan(num2, num))
				{
					num = num2;
					result = RevitDocumentExtension.GetEndPoint(curve, 0);
				}
			}
			return result;
		}

		private void CreateWindowDoorSizeDim(List<Dimension> lstRetDim, OutWallThreeDimData dimData, XYZ ptDim)
		{
			Transaction transaction = new Transaction(this.m_doc, "newWinDoorDim");
			transaction.Start();
			try
			{
				List<List<Wall>> list = new List<List<Wall>>();
				this.GetSpaceWallGroup(dimData, list);
				List<List<Wall>> list2 = new List<List<Wall>>();
				double dCmpDis = 9.84251968503937;
				foreach (List<Wall> list3 in list)
				{
					foreach (Wall wall in list3)
					{
						if (!this.HasWallArr(wall, list2))
						{
							List<Wall> list4 = new List<Wall>();
							this.GetOneWallLess3MWalls(dCmpDis, list3, wall, list4);
							if (list4.Count > 0)
							{
								list2.Add(list4);
							}
						}
					}
				}
				foreach (List<Wall> list5 in list2)
				{
					bool flag = this.IsThreeSizeDimWallArr(dimData.GridData.ptDim, list5, dimData.GridData.LstParallelGrid);
					if (dimData.GridData.ptDim != null && flag)
					{
						if (!OutWallThirdSizeDimXDataUtil.HasExistDimesion(list5, this.m_view))
						{
							ReferenceArray windowDoorDimReferences = this.GetWindowDoorDimReferences(this.m_uiApp, list5, false);
							XYZ xyz = dimData.DirOutWallData.FursthestWall.Orientation.Normalize();
							xyz = YJKArch.Utils.Geometry.RotateTo(xyz, Math.PI*.5, XYZ.BasisZ);
							Dimension dimension = this.CreateSizeDim(windowDoorDimReferences, xyz, ptDim);
							if (dimension != null)
							{
								lstRetDim.Add(dimension);
								foreach (Wall ele in list5)
								{
									OutWallThirdSizeDimXDataUtil.WriteXData(ele, dimension.UniqueId);
								}
							}
						}
						List<Wall> vertWall = WallOperattionUtil.GetVertWall(list5, dimData.DirOutWallData.LstVertWall);
						if (vertWall.Count > 0)
						{
							List<Dimension> list6 = new OutWallThickDimManager(this.m_uiApp, this.m_doc, this.m_view, vertWall, dimData.GridData.LstIntersectGrid, null).CreateOutWallThickDim();
							if (list6.Count > 0)
							{
								lstRetDim.AddRange(list6);
							}
						}
					}
					else if (!OutWallThirdSizeDimXDataUtil.HasExistDimesion(list5, this.m_view))
					{
						bool bIsDoorWin = true;
						Wall wall2 = list5.First<Wall>();
						ptDim = WallOperattionUtil.GetWallLocationCenter(wall2);
						ptDim += wall2.Orientation.Normalize().Multiply(1.9685039370078741 + wall2.Width / 2.0);
						ReferenceArray windowDoorDimReferences2 = this.GetWindowDoorDimReferences(this.m_uiApp, list5, bIsDoorWin);
						XYZ xyz2 = dimData.DirOutWallData.FursthestWall.Orientation.Normalize();
						xyz2 = YJKArch.Utils.Geometry.RotateTo(xyz2, Math.PI*.5, XYZ.BasisZ);
						Dimension dimension2 = this.CreateSizeDim(windowDoorDimReferences2, xyz2, ptDim);
						if (dimension2 != null)
						{
							lstRetDim.Add(dimension2);
							foreach (Wall ele2 in list5)
							{
								OutWallThirdSizeDimXDataUtil.WriteXData(ele2, dimension2.UniqueId);
							}
						}
					}
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
			}
		}

		private bool IsThreeSizeDimWallArr(XYZ pt, List<Wall> lstWall, List<Grid> lstGrid)
		{
			try
			{
				foreach (Wall wall in lstWall)
				{
					Curve pointToWallCurve = WallOperattionUtil.GetPointToWallCurve(pt, wall);
					if (this.IsTwoKua(pointToWallCurve, lstGrid))
					{
						return true;
					}
				}
			}
			catch (Exception)
			{
				return false;
			}
			return false;
		}

		private void GetOneWallLess3MWalls(double dCmpDis, List<Wall> wallArr, Wall wall, List<Wall> lst3MWall)
		{
			if (!this.HasSplitWall(lst3MWall, wall))
			{
				lst3MWall.Add(wall);
			}
			List<Wall> list = new List<Wall>();
			foreach (Wall wall2 in wallArr)
			{
				if (wall2.Id.IntegerValue != wall.Id.IntegerValue && !this.HasSplitWall(lst3MWall, wall2) && YJKArch.Utils.Geometry.Lessthan_Or_Equal(WallOperattionUtil.GetTwoParallelWallMinDis(wall2, wall), dCmpDis, 1E-06))
				{
					list.Add(wall2);
					lst3MWall.Add(wall2);
				}
			}
			if (list.Count > 0)
			{
				foreach (Wall wall3 in list)
				{
					this.GetOneWallLess3MWalls(dCmpDis, wallArr, wall3, lst3MWall);
				}
			}
		}

		private void GetSpaceWallGroup(OutWallThreeDimData dimData, List<List<Wall>> lstSpaceWallArr)
		{
			List<Wall> list = new List<Wall>();
			new List<Wall>();
			List<Wall> lstParallelWall = dimData.DirOutWallData.LstParallelWall;
			lstParallelWall.Reverse();
			foreach (Wall wall in lstParallelWall)
			{
				if (WallOperattionUtil.WallHasWinOrDoor(wall) && !this.HasSplitWall(list, wall))
				{
					list.Add(wall);
					List<Wall> list2 = new List<Wall>();
					list2.Add(wall);
					foreach (Wall wall2 in dimData.DirOutWallData.LstParallelWall)
					{
						if (WallOperattionUtil.WallHasWinOrDoor(wall2) && !this.HasSplitWall(list, wall2))
						{
							if (YJKArch.Utils.Geometry.IsEqual(WallOperattionUtil.GetTwoWallDistance(wall, wall2), 0.0))
							{
								list2.Add(wall2);
								list.Add(wall2);
							}
							else
							{
								Curve twoParallelWallCurve = WallOperattionUtil.GetTwoParallelWallCurve(wall, wall2);
								if (this.IsTwoKua(twoParallelWallCurve, dimData.GridData.LstParallelGrid))
								{
									list2.Add(wall2);
									list.Add(wall2);
								}
							}
						}
					}
					lstSpaceWallArr.Add(list2);
				}
			}
		}

		private bool IsTwoKua(Curve curve, List<Grid> lstGrid)
		{
			try
			{
				Curve zeroHeightCurve = OutWallThreeDimManager.GetZeroHeightCurve(curve);
				int num = 0;
				foreach (Grid grid in lstGrid)
				{
					IntersectionResultArray intersectionResultArray = null;
					OutWallThreeDimManager.GetZeroHeightCurve(grid.Curve).Intersect(zeroHeightCurve, out intersectionResultArray);
					if (intersectionResultArray != null && intersectionResultArray.Size == 1)
					{
						num++;
					}
				}
				if (num < 3)
				{
					return true;
				}
			}
			catch (Exception)
			{
				return false;
			}
			return false;
		}

		public static Curve GetZeroHeightCurve(Curve boundCv)
		{
			Transform transform = Transform.CreateTranslation(new XYZ(0.0, 0.0, -boundCv.GetEndPoint(0).Z));
			return boundCv.CreateTransformed(transform);
		}

		private void CreateGridSizeDim(List<Dimension> lstRetDim, OutWallThreeDimData dimData, XYZ ptDim)
		{
			Transaction transaction = new Transaction(this.m_doc, "newGridDim");
			transaction.Start();
			try
			{
				if (dimData.GridData.ptDim != null && dimData.GridData.LstIntersectGrid.Count > 1)
				{
					ReferenceArray referenceArray = new ReferenceArray();
					foreach (Grid grid in dimData.GridData.LstIntersectGrid)
					{
						Reference reference = null;
						GridOperationUtil.GetGridDimRef(this.m_uiApp, grid, out reference);
						if (reference != null)
						{
							referenceArray.Append(reference);
						}
					}
					ptDim = new XYZ(ptDim.X, ptDim.Y, dimData.GridData.ptDim.Z);
					XYZ xyz = dimData.DirOutWallData.FursthestWall.Orientation.Normalize();
					xyz = YJKArch.Utils.Geometry.RotateTo(xyz, Math.PI*.5, XYZ.BasisZ);
					if (!OutWallSecondSizeDimXDataUtil.HasExistDimesion(new List<Wall>
					{
						dimData.DirOutWallData.FursthestWall
					}, this.m_view))
					{
						Dimension dimension = this.CreateSizeDim(referenceArray, xyz, ptDim);
						if (dimension != null)
						{
							lstRetDim.Add(dimension);
							OutWallSecondSizeDimXDataUtil.WriteXData(dimData.DirOutWallData.FursthestWall, dimension.UniqueId);
						}
					}
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
			}
		}

		private void CreateOutWallProfileDim(List<Dimension> lstRetDim, OutWallThreeDimData dimData, XYZ ptDim)
		{
			Transaction transaction = new Transaction(this.m_doc, "newProfileDim");
			transaction.Start();
			try
			{
				if (dimData.DirOutWallData.LstVertWall.Count == 2 && dimData.GridData.ptDim != null)
				{
					ReferenceArray referenceArray = new ReferenceArray();
					Reference wallProfileRef = WallOperattionUtil.GetWallProfileRef(this.m_uiApp, dimData.DirOutWallData.LstVertWall.First<Wall>());
					if (wallProfileRef != null)
					{
						referenceArray.Append(wallProfileRef);
					}
					wallProfileRef = WallOperattionUtil.GetWallProfileRef(this.m_uiApp, dimData.DirOutWallData.LstVertWall.Last<Wall>());
					if (wallProfileRef != null)
					{
						referenceArray.Append(wallProfileRef);
					}
					ptDim = new XYZ(ptDim.X, ptDim.Y, dimData.GridData.ptDim.Z);
					XYZ xyz = dimData.DirOutWallData.FursthestWall.Orientation.Normalize();
					xyz = YJKArch.Utils.Geometry.RotateTo(xyz, Math.PI*.5, XYZ.BasisZ);
					if (!OutWallFirstSizeDimXDataUtil.HasExistDimesion(new List<Wall>
					{
						dimData.DirOutWallData.FursthestWall
					}, this.m_view))
					{
						Dimension dimension = this.CreateSizeDim(referenceArray, xyz, ptDim);
						if (dimension != null)
						{
							lstRetDim.Add(dimension);
							OutWallFirstSizeDimXDataUtil.WriteXData(dimData.DirOutWallData.FursthestWall, dimension.UniqueId);
						}
					}
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
			}
		}

		private Dimension CreateSizeDim(ReferenceArray refArr, XYZ vtDim, XYZ ptDim)
		{
			Line line = YJKLineEx.YJKGetBound(ptDim, ptDim + vtDim * 10.0);
			DimensionType dimensionType = RevitDimensionUtiltyMethod.GetDimensionType(this.m_doc, 0, null);
			return this.m_doc.Create.NewDimension(this.m_view, line, refArr, dimensionType);
		}

		private List<ReferenceInfo> GetWindowDoorDimReferences(UIApplication uiApp, Wall wall, bool bIsDoorWin = false)
		{
			if (!WallOperattionUtil.WallHasWinOrDoor(wall))
			{
				return null;
			}
			List<ReferenceInfo> list = new List<ReferenceInfo>();
			foreach (FamilyInstance opening in WallOperattionUtil.GetWallDoorWindow(wall))
			{
				List<ReferenceInfo> list2 = new List<ReferenceInfo>();
				this.GetOpeningSideDimRef(opening, ref list2);
				if (list2.Count > 1)
				{
					list.AddRange(list2);
				}
			}
			return list;
		}

		private void GetOpeningSideDimRef(FamilyInstance opening, ref List<ReferenceInfo> lstDimRef)
		{
			Element host = opening.Host;
			if (host == null)
			{
				return;
			}
			Wall wall = host as Wall;
			if (wall == null)
			{
				return;
			}
			XYZ xyz = null;
			XYZ xyz2 = null;
			XYZ xyz3 = null;
			this.GetOpeningPoint(opening, ref xyz, ref xyz2, ref xyz3);
			double num = AssistFunc.mmToFeet(1.0);
			foreach (object obj in ElementAssistFunc.GetElementFaces(this.m_uiApp, this.m_doc, wall))
			{
				Face face = (Face)obj;
				PlanarFace planarFace = face as PlanarFace;
				if (!(planarFace == null) && DimPublicFunc.IsParallel(RevitElementExtension.FaceNormal(planarFace), xyz2 - xyz))
				{
					XYZ origin = planarFace.Origin;
					XYZ xyz4 = YJKArch.Utils.Geometry.RotateTo(RevitElementExtension.FaceNormal(planarFace), Math.PI*.5, XYZ.BasisZ);
					XYZ xyzpoint = Line.CreateUnbound(new XYZ(origin.X, origin.Y, xyz.Z), xyz4).Project(xyz).XYZPoint;
					if (YJKArch.Utils.Geometry.Is_Point_OnSegment(xyz, xyz2, xyzpoint, num))
					{
						ReferenceInfo referenceInfo = new ReferenceInfo(face.Reference, DimPublicFunc.To2d(xyzpoint));
						if (!DimPublicFunc.HasExistRefInfo(lstDimRef, referenceInfo))
						{
							lstDimRef.Add(referenceInfo);
						}
					}
				}
			}
			if (lstDimRef.Count > 2)
			{
				xyz = DimPublicFunc.To2d(xyz);
				xyz2 = DimPublicFunc.To2d(xyz2);
				xyz3 = DimPublicFunc.To2d(xyz3);
				ReferenceInfo referenceInfo2 = null;
				ReferenceInfo referenceInfo3 = null;
				foreach (ReferenceInfo referenceInfo4 in lstDimRef)
				{
					XYZ xyz5 = referenceInfo4.ThePosition - xyz3;
					if (xyz5.AngleTo(xyz - xyz3) < 0.003)
					{
						if (referenceInfo2 == null || xyz3.DistanceTo(referenceInfo2.ThePosition) > xyz3.DistanceTo(referenceInfo4.ThePosition))
						{
							referenceInfo2 = referenceInfo4;
						}
					}
					else if (xyz5.AngleTo(xyz2 - xyz3) < 0.003 && (referenceInfo3 == null || xyz3.DistanceTo(referenceInfo3.ThePosition) > xyz3.DistanceTo(referenceInfo4.ThePosition)))
					{
						referenceInfo3 = referenceInfo4;
					}
				}
				if (referenceInfo2 != null && referenceInfo3 != null)
				{
					lstDimRef = new List<ReferenceInfo>();
					lstDimRef.Add(referenceInfo2);
					lstDimRef.Add(referenceInfo3);
				}
			}
		}

		private bool GetOpeningPoint(FamilyInstance opening, ref XYZ ptEnd1, ref XYZ ptEnd2, ref XYZ ptCenter)
		{
			double num = 0.01;
			if (opening.Category.Id.IntegerValue == -2000023)
			{
				num = RevitElementExtension.GetParameter(opening.Symbol, BuiltInParameter.CASEWORK_WIDTH).AsDouble();
			}
			else if (opening.Category.Id.IntegerValue == -2000014)
			{
				num = RevitElementExtension.GetParameter(opening.Symbol, BuiltInParameter.CASEWORK_WIDTH).AsDouble();
			}
			else if (opening.Category.Id.IntegerValue == -2001350)
			{
				Parameter parameter = RevitElementExtension.GetParameter(opening, "门宽");
				if (parameter != null)
				{
					num = parameter.AsDouble();
				}
			}
			ptCenter = (opening.Location as LocationPoint).Point;
			ptEnd1 = ptCenter + opening.HandOrientation * num / 2.0;
			ptEnd2 = ptCenter - opening.HandOrientation * num / 2.0;
			Line line = YJKLineEx.YJKGetBound(ptEnd1, ptEnd2);
			foreach (Curve curve in this.GetElementCurves(opening))
			{
				if (!(curve.GetType() != typeof(Line)))
				{
					Line line2 = curve as Line;
					if (DimPublicFunc.IsVertical(line2.Direction, opening.HandOrientation))
					{
						XYZ hyendPoint = RevitDocumentExtension.GetEndPoint(line2, 0);
						XYZ hyendPoint2 = RevitDocumentExtension.GetEndPoint(line2, 1);
						if (YJKArch.Utils.Geometry.IsEqual(hyendPoint.Z, hyendPoint2.Z))
						{
							XYZ xyz = YJKArch.Utils.Geometry.IntersectWithTwoCurves(DimPublicFunc.ChangeLineElevation(line2, ptEnd1.Z), line, ptEnd1, false, true, this.m_uiApp.Application);
							if (xyz != null)
							{
								DimPublicFunc.ExtendLine(xyz, ref ptEnd1, ref ptEnd2);
							}
						}
					}
				}
			}
			return true;
		}

		private List<Curve> GetElementCurves(Element element)
		{
			List<Curve> list = new List<Curve>();
			List<GeometryObject> geometryObjectArray = RevitVersionFuncs.GetGeometryObjectArray(element.get_Geometry(new Options
			{
				ComputeReferences = true,
				IncludeNonVisibleObjects = false,
				View = element.Document.ActiveView
			}));
			for (int i = 0; i < geometryObjectArray.Count; i++)
			{
				GeometryObject geometryObject = geometryObjectArray[i];
				if (geometryObject is GeometryInstance)
				{
					GeometryInstance geoInstance = geometryObject as GeometryInstance;
					this.GetGeometryInstanceLines(geoInstance, ref list);
				}
				else if (geometryObject is Solid)
				{
					Solid solid = geometryObject as Solid;
					this.GetSolidLines(solid, ref list);
				}
				else if (geometryObject is Curve)
				{
					list.Add(geometryObject as Curve);
				}
			}
			return list;
		}

		private void GetGeometryInstanceLines(GeometryInstance geoInstance, ref List<Curve> lstCurve)
		{
			foreach (object obj in RevitVersionFuncs.GetGeometryObjectArray(geoInstance.GetInstanceGeometry()))
			{
				if (obj is Solid)
				{
					Solid solid = obj as Solid;
					this.GetSolidLines(solid, ref lstCurve);
				}
				else if (obj is Curve)
				{
					lstCurve.Add(obj as Curve);
				}
			}
		}

		private void GetSolidLines(Solid solid, ref List<Curve> lstCurve)
		{
			if (solid != null)
			{
				EdgeArray edges = solid.Edges;
				for (int i = 0; i < edges.Size; i++)
				{
                    Edge edge = edges.get_Item(i);
					if (edge.Tessellate().Count == 2)
					{
						lstCurve.Add(edge.AsCurve());
					}
				}
			}
		}

		private List<Element> GetColumnsOnLine(XYZ pt1, XYZ pt2)
		{
			XYZ xyz = new XYZ(Math.Min(pt1.X, pt2.X), Math.Min(pt1.Y, pt2.Y), pt1.Z);
			XYZ xyz2 = new XYZ(Math.Max(pt1.X, pt2.X), Math.Max(pt1.Y, pt2.Y), pt1.Z + 30.0);
			BoundingBoxIntersectsFilter boundingBoxIntersectsFilter = new BoundingBoxIntersectsFilter(new Outline(xyz, xyz2));
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(this.m_doc);
			List<Element> list = new List<Element>();
			List<Element> list2 = filteredElementCollector.OfCategory(BuiltInCategory.OST_Columns).WherePasses(boundingBoxIntersectsFilter).ToList<Element>();
			List<Element> list3 = filteredElementCollector2.OfCategory(BuiltInCategory.OST_StructuralColumns).WherePasses(boundingBoxIntersectsFilter).ToList<Element>();
			if (list2 != null)
			{
				list.AddRange(list2);
			}
			if (list3 != null)
			{
				list.AddRange(list3);
			}
			for (int i = 0; i < list.Count; i++)
			{
				if (list[i].LevelId != this.m_doc.ActiveView.GenLevel.Id)
				{
					list.RemoveAt(i);
					i--;
				}
			}
			return list;
		}

		private ReferenceArray GetWindowDoorDimReferences(UIApplication uiApp, List<Wall> lstWall, bool bIsDoorWin = false)
		{
			ReferenceArray referenceArray = new ReferenceArray();
			List<ReferenceInfo> list = new List<ReferenceInfo>();
			foreach (Wall wall in lstWall)
			{
				List<ReferenceInfo> windowDoorDimReferences = this.GetWindowDoorDimReferences(this.m_uiApp, wall, false);
				if (windowDoorDimReferences != null && windowDoorDimReferences.Count > 0)
				{
					list.AddRange(windowDoorDimReferences);
				}
			}
			XYZ xyz = null;
			XYZ xyz2 = null;
			WallOperattionUtil.GetLstWallMaxPoint(lstWall, out xyz, out xyz2);
			xyz = DimPublicFunc.To2d(xyz);
			xyz2 = DimPublicFunc.To2d(xyz2);
			List<ReferenceInfo> list2 = new List<ReferenceInfo>();
			Line crossLine = YJKLineEx.YJKGetBound(xyz, xyz2);
			XYZ xyz3 = (xyz2 - xyz).Normalize().Multiply(AssistFunc.mmToFeet(150.0));
			List<Grid> list3 = DimPublicFunc.LineCrossGrids(uiApp, xyz - xyz3, xyz2 + xyz3, true);
			if (list3 != null && list3.Count > 0)
			{
				using (List<Grid>.Enumerator enumerator2 = list3.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						Grid grid = enumerator2.Current;
						DimPublicFunc.GetGridDimRef(uiApp, grid, crossLine, ref list2);
					}
					goto IL_1A4;
				}
			}
			if (lstWall.Count == 1)
			{
				XYZ xyz4 = null;
				XYZ xyz5 = null;
				WallOperattionUtil.GetWallStartEndPoint(lstWall.First<Wall>(), out xyz4, out xyz5);
				XYZ vecTrans = Transform.CreateRotation(XYZ.BasisZ, Math.PI*.5).OfVector(lstWall.First<Wall>().Orientation.Normalize());
				if (xyz4 != null)
				{
					Grid nearestPointGrid = GridOperationUtil.GetNearestPointGrid(this.m_uiApp, xyz4, vecTrans);
					if (nearestPointGrid != null)
					{
						DimPublicFunc.GetGridDimRef(uiApp, nearestPointGrid, crossLine, ref list2);
					}
				}
				if (xyz5 != null)
				{
					Grid nearestPointGrid2 = GridOperationUtil.GetNearestPointGrid(this.m_uiApp, xyz5, vecTrans);
					if (nearestPointGrid2 != null)
					{
						DimPublicFunc.GetGridDimRef(uiApp, nearestPointGrid2, crossLine, ref list2);
					}
				}
			}
			IL_1A4:
			if (list2.Count > 0)
			{
				list.AddRange(list2);
			}
			List<ReferenceInfo> twoEndWallReference = this.GetTwoEndWallReference(uiApp, lstWall, true);
			if (twoEndWallReference.Count > 0 && !bIsDoorWin)
			{
				list.AddRange(twoEndWallReference);
			}
			if (list.Count > 0)
			{
				Curve curve = (lstWall.First<Wall>().Location as LocationCurve).Curve;
				curve.MakeUnbound();
				for (int i = 0; i < list.Count; i++)
				{
					ReferenceInfo referenceInfo = list[i];
					for (int j = i + 1; j < list.Count; j++)
					{
						XYZ xyzpoint = curve.Project(referenceInfo.ThePosition).XYZPoint;
						XYZ xyzpoint2 = curve.Project(list[j].ThePosition).XYZPoint;
						if (xyzpoint.DistanceTo(xyzpoint2) < 0.001)
						{
							list.RemoveAt(j);
							j--;
						}
					}
				}
				foreach (ReferenceInfo referenceInfo2 in list)
				{
					XYZ xyz6 = new XYZ(referenceInfo2.ThePosition.X, referenceInfo2.ThePosition.Y, this.m_view.GenLevel.Elevation);
					XYZ xyz7 = -lstWall.First<Wall>().Orientation.Normalize().Multiply(AssistFunc.mmToFeet(1.0));
					XYZ xyz8 = xyz6 + xyz7;
					Line line = Line.CreateBound(xyz6, xyz8);
					ModelCurve modelCurve = this.m_doc.Create.NewModelCurve(line, this.m_view.SketchPlane);
					if (modelCurve != null)
					{
						referenceArray.Append(modelCurve.GeometryCurve.Reference);
					}
				}
			}
			return referenceArray;
		}

		private List<ReferenceInfo> GetTwoEndWallReference(UIApplication uiApp, List<Wall> lstWall, bool bIsMaxDis = true)
		{
			List<ReferenceInfo> list = new List<ReferenceInfo>();
			XYZ xyz = null;
			XYZ xyz2 = null;
			WallOperattionUtil.GetWallStartEndPoint(lstWall.First<Wall>(), out xyz, out xyz2);
			Line line = null;
			if (xyz != null && xyz2 != null)
			{
				line = Line.CreateBound(xyz, xyz2);
			}
			if (null == line)
			{
				return list;
			}
			line.MakeUnbound();
			List<XYZ> list2 = new List<XYZ>();
			Dictionary<XYZ, XYZ> dictionary = new Dictionary<XYZ, XYZ>();
			foreach (Wall wall in lstWall)
			{
				XYZ xyz3 = null;
				XYZ xyz4 = null;
				WallOperattionUtil.GetWallStartEndPoint(wall, out xyz3, out xyz4);
				if (xyz3 != null && xyz4 != null)
				{
					XYZ xyzpoint = line.Project(xyz3).XYZPoint;
					list2.Add(xyzpoint);
					dictionary.Add(xyzpoint, xyz3);
					xyzpoint = line.Project(xyz4).XYZPoint;
					list2.Add(xyzpoint);
					dictionary.Add(xyzpoint, xyz4);
				}
			}
			List<XYZ> source = this.SortPointsWithVector(list2, line.Direction.Normalize());
			XYZ xyz5 = dictionary[source.First<XYZ>()];
			XYZ xyz6 = dictionary[source.Last<XYZ>()];
			if (xyz5 == null || xyz6 == null)
			{
				return list;
			}
			Wall wall2 = null;
			Wall wall3 = null;
			XYZ xyz7 = (source.First<XYZ>() - source.Last<XYZ>()).Normalize();
			Line crossLine = Line.CreateBound(xyz5 + xyz7.Multiply(AssistFunc.mmToFeet(200.0)), xyz5 - xyz7.Multiply(AssistFunc.mmToFeet(200.0)));
			DimPublicFunc.NearWallByPoint(uiApp, xyz5, crossLine, out wall2);
			crossLine = Line.CreateBound(xyz6 + xyz7.Multiply(AssistFunc.mmToFeet(200.0)), xyz6 - xyz7.Multiply(AssistFunc.mmToFeet(200.0)));
			DimPublicFunc.NearWallByPoint(uiApp, xyz6, crossLine, out wall3);
			if (wall2 == null || wall3 == null)
			{
				return list;
			}
			Dictionary<XYZ, Reference> dictionary2 = new Dictionary<XYZ, Reference>();
			WallOperattionUtil.GetOutWallThicknessRefDic(uiApp, wall2, ref dictionary2);
			WallOperattionUtil.GetOutWallThicknessRefDic(uiApp, wall3, ref dictionary2);
			List<XYZ> list3 = new List<XYZ>();
			list3.AddRange(dictionary2.Keys);
			List<XYZ> list4 = this.SortPointsWithVector(list3, line.Direction.Normalize());
			if (bIsMaxDis)
			{
				ReferenceInfo item = new ReferenceInfo(dictionary2[list4.First<XYZ>()], DimPublicFunc.To2d(line.Project(list4.First<XYZ>()).XYZPoint));
				list.Add(item);
				item = new ReferenceInfo(dictionary2[list4.Last<XYZ>()], DimPublicFunc.To2d(line.Project(list4.Last<XYZ>()).XYZPoint));
				list.Add(item);
			}
			else
			{
				foreach (XYZ xyz8 in list4)
				{
					ReferenceInfo item2 = new ReferenceInfo(dictionary2[xyz8], DimPublicFunc.To2d(line.Project(xyz8).XYZPoint));
					list.Add(item2);
				}
			}
			return list;
		}

		private bool HasReference(ReferenceArray arReference, Reference Ref, XYZ vecDim)
		{
			foreach (object obj in arReference)
			{
				Reference reference = (Reference)obj;
				XYZ zero = XYZ.Zero;
				vecDim = YJKArch.Utils.Geometry.RotateTo(vecDim, Math.PI*.5, XYZ.BasisZ);
				XYZ xyz = zero + vecDim.Multiply(10.0);
				Line line = Line.CreateBound(zero, xyz);
				ReferenceArray referenceArray = new ReferenceArray();
				referenceArray.Append(reference);
				referenceArray.Append(Ref);
				if (YJKArch.Utils.Geometry.IsEqual(this.m_doc.Create.NewDimension(this.m_view, line, referenceArray).Value.Value, 0.0))
				{
					return true;
				}
			}
			return false;
		}

		private bool HasWallArr(Wall wall, List<List<Wall>> lstWallArr)
		{
			foreach (List<Wall> lstSplitWall in lstWallArr)
			{
				if (this.HasSplitWall(lstSplitWall, wall))
				{
					return true;
				}
			}
			return false;
		}

		private bool HasGridArr(Grid grid, List<List<Grid>> lstGridArr)
		{
			foreach (List<Grid> list in lstGridArr)
			{
				using (List<Grid>.Enumerator enumerator2 = list.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						if (enumerator2.Current.Id.IntegerValue == grid.Id.IntegerValue)
						{
							return true;
						}
					}
				}
			}
			return false;
		}

		private Document m_doc;

		private UIApplication m_uiApp;

		private Autodesk.Revit.DB.View m_view;

		private List<WallDataEx> m_lstWallDataEx = new List<WallDataEx>();

		private const double m_d3M = 9.84251968503937;

		private const double m_d2400MM = 7.8740157480314963;

		private const double m_d600MM = 1.9685039370078741;

		private const double m_d800MM = 2.6246719160104992;

		private const double m_d4800MM = 15.748031496062991;

		private const double m_d4000MM = 15.748031496062991;
	}
}
